package com.sojson.controller.po;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.controller.po.param.verify.CouponUserParamVerify;
import com.sojson.entity.po.CouponUser;
import com.sojson.result.Result;
import com.sojson.service.CouponUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 券可用用户控制器
 * 
 * @author liu
 * @date 2021-06-02
 */
@Api(tags = "券可用用户管理")
@RestController
@RequestMapping("/couponUser")
public class CouponUserController {

    @Autowired
    private CouponUserService couponUserService;

    /**
     * 单条插入
     * 
     * @param couponUser
     * @return
     */
    @PostMapping("/insert")
    @PreAuthorize("@ss.hasPermi('/couponUser/insert')")
    @ApiOperation("给券可用用户添加一条数据")
    public Result<Integer> insert(@RequestBody CouponUser couponUser) {
        // 校验参数
        Result<Integer> paramResult = CouponUserParamVerify.insert(couponUser);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        couponUser.insertInit();
        return Result.success(couponUserService.insert(couponUser));
    }

    /**
     * 批量插入
     * 
     * @param couponUser
     * @return
     */
    @PostMapping("/insertList")
    @PreAuthorize("@ss.hasPermi('/couponUser/insertList')")
    @ApiOperation("给券可用用户添加多条数据")
    public Result<Integer> insertList(@RequestBody CouponUser couponUser) {
        return Result.success(couponUserService.insertList(couponUser));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param couponUser
     * @return
     */
    @DeleteMapping("/deleteByDto")
    @PreAuthorize("@ss.hasPermi('/couponUser/deleteByDto')")
    @ApiOperation("通过对象批量删除券可用用户数据(物理删除)")
    public Result<Integer> deleteByDto(@RequestBody CouponUser couponUser) {
        return Result.success(couponUserService.deleteByDto(couponUser));
    }

    /**
     * 统计全部数量
     * 
     * @return
     */
    @GetMapping("/countAll")
    @PreAuthorize("@ss.hasPermi('/couponUser/countAll')")
    @ApiOperation("统计券可用用户全部数据(包含逻辑删除的)")
    public Result<Integer> countAll() {
        return Result.success(couponUserService.countAll());
    }

    /**
     * 查询所有
     * 
     * @return
     */
    @GetMapping("/listAll")
    @PreAuthorize("@ss.hasPermi('/couponUser/listAll')")
    @ApiOperation("查询券可用用户所有数据(包含逻辑删除的)")
    public Result<List<CouponUser>> listAll() {
        return Result.success(couponUserService.listAll());
    }

    /**
     * 根据对象统计数量
     * 
     * @param couponUser
     * @return
     */
    @GetMapping("/countByDto")
    @PreAuthorize("@ss.hasPermi('/couponUser/countByDto')")
    @ApiOperation("根据对象统计券可用用户数据")
    public Result<Integer> countByDto(CouponUser couponUser) {
        return Result.success(couponUserService.countByDto(couponUser));
    }

    /**
     * 根据对象查询
     * 
     * @param couponUser
     * @return
     */
    @GetMapping("/listByDto")
    @PreAuthorize("@ss.hasPermi('/couponUser/listByDto')")
    @ApiOperation("根据对象查询券可用用户数据")
    public Result<List<CouponUser>> listByDto(CouponUser couponUser) {
        return Result.success(couponUserService.listByDto(couponUser));
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("@ss.hasPermi('/couponUser/deleteById/{id}')")
    @ApiOperation("通过id删除券可用用户数据(物理删除)")
    @ApiImplicitParam(name = "id", value = "id")
    public Result<Integer> deleteById(@PathVariable Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(couponUserService.deleteById(id));
    }

    /**
     * 通过id数组删除(物理删除)
     * 
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds/{ids}")
    @PreAuthorize("@ss.hasPermi('/couponUser/deleteByIds/{ids}')")
    @ApiOperation("通过id数组删除券可用用户数据(物理删除)")
    @ApiImplicitParam(name = "ids", value = "id数组")
    public Result<Integer> deleteByIds(@PathVariable Integer[] ids) {
        if (ids.length == 0) {
            return Result.success();
        }

        return Result.success(couponUserService.deleteByIds(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param couponUser
     * @return
     */
    @PutMapping("/updateAllById")
    @PreAuthorize("@ss.hasPermi('/couponUser/updateAllById')")
    @ApiOperation("通过id更新券可用用户数据(全部更新)")
    public Result<Integer> updateAllById(@RequestBody CouponUser couponUser) {
        return Result.success(couponUserService.updateAllById(couponUser));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param couponUser
     * @return
     */
    @PutMapping("/updateAllByIdList")
    @PreAuthorize("@ss.hasPermi('/couponUser/updateAllByIdList')")
    @ApiOperation("通过id列表更新券可用用户数据(全部更新)(更新内容相同)")
    public Result<Integer> updateAllByIdList(@RequestBody CouponUser couponUser) {
        return Result.success(couponUserService.updateAllByIdList(couponUser));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param couponUser
     * @return
     */
    @PutMapping("/updateDtoListAllById")
    @PreAuthorize("@ss.hasPermi('/couponUser/updateDtoListAllById')")
    @ApiOperation("通过id列表更新券可用用户数据(全部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListAllById(@RequestBody CouponUser couponUser) {
        return Result.success(couponUserService.updateDtoListAllById(couponUser));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param couponUser
     * @return
     */
    @PutMapping("/updateById")
    @PreAuthorize("@ss.hasPermi('/couponUser/updateById')")
    @ApiOperation("通过id更新券可用用户数据(局部更新)")
    public Result<Integer> updateById(@RequestBody CouponUser couponUser) {
        // 校验参数
        Result<Integer> paramResult = CouponUserParamVerify.updateById(couponUser);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        couponUser.updateInit();
        couponUser.setUpdateNull();
        return Result.success(couponUserService.updateById(couponUser));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param couponUser
     * @return
     */
    @PutMapping("/updateByIdList")
    @PreAuthorize("@ss.hasPermi('/couponUser/updateByIdList')")
    @ApiOperation("通过id列表更新券可用用户数据(局部更新)(更新内容相同)")
    public Result<Integer> updateByIdList(@RequestBody CouponUser couponUser) {
        return Result.success(couponUserService.updateByIdList(couponUser));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param couponUser
     * @return
     */
    @PutMapping("/updateDtoListById")
    @PreAuthorize("@ss.hasPermi('/couponUser/updateDtoListById')")
    @ApiOperation("通过id列表更新券可用用户数据(局部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListById(@RequestBody CouponUser couponUser) {
        return Result.success(couponUserService.updateDtoListById(couponUser));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @PreAuthorize("@ss.hasPermi('/couponUser/findById/{id}')")
    @ApiOperation("通过id查询券可用用户数据")
    @ApiImplicitParam(name = "id", value = "id")
    public Result<CouponUser> findById(@PathVariable Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(couponUserService.findById(id));
    }

    /**
     * 通过id数组查询
     * 
     * @param ids
     * @return
     */
    @GetMapping("/findByIds/{ids}")
    @PreAuthorize("@ss.hasPermi('/couponUser/findByIds/{ids}')")
    @ApiOperation("通过id数组查询券可用用户数据")
    @ApiImplicitParam(name = "ids", value = "id数组")
    public Result<List<CouponUser>> findByIds(@PathVariable Integer[] ids) {
        if (ids.length == 0) {
            return Result.success();
        }

        return Result.success(couponUserService.findByIds(ids));
    }

    /**
     * 通过对象查询id列表
     * 
     * @param couponUser
     * @return
     */
    @GetMapping("/findIdByDto")
    @PreAuthorize("@ss.hasPermi('/couponUser/findIdByDto')")
    @ApiOperation("通过对象查询券可用用户id列表")
    public Result<List<Integer>> findIdByDto(CouponUser couponUser) {
        return Result.success(couponUserService.findIdByDto(couponUser));
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     */
    @DeleteMapping("/clear")
    @PreAuthorize("@ss.hasPermi('/couponUser/clear')")
    @ApiOperation("清空券可用用户数据(物理清空)")
    public Result<Integer> clear() {
        return Result.success(couponUserService.clear());
    }

    /**
     * 根据对象查询
     * 
     * @param couponUser
     * @return
     */
    @GetMapping(value = "/list")
    @PreAuthorize("@ss.hasPermi('/couponUser/list')")
    public Object list(CouponUser couponUser) {
        List<CouponUser> list = couponUserService.list(couponUser);
        return Result.success(list);
    }

}